﻿using System;
using System.Windows.Forms;
using TFSRequestManager.TfsAdministration;

namespace TFSRequestManager
{
    public partial class DetailsForm : Form
    {
        #region Constructor

        public DetailsForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public TeamFoundationRequestInformation ActiveRequest { get; set; }

        public Guid HostId { get; set; }

        public string ServerActivityName { get; set; }

        public DateTime ServerActivityStartTime { get; set; }

        public int ServerActivityStatus { get; set; }

        public string ServerActivityStatusReason { get; set; }

        public string RemoteAddress { get; set; }

        #endregion

        #region Event Handlers

        private void DetailsForm_Load(object sender, EventArgs e)
        {
            if (ActiveRequest != null)
            {
                this.Text += string.Format(" [{0}]", ActiveRequest.RequestId);
                ListViewItem listItem;

                listItem = new ListViewItem("Request Id");
                listItem.SubItems.Add(ActiveRequest.RequestId.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Method Name");
                listItem.SubItems.Add(ActiveRequest.MethodName);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("User Name");
                listItem.SubItems.Add(ActiveRequest.UserName);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Start Time");
                listItem.SubItems.Add(ActiveRequest.StartTime.ToLocalTime().ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Execution Time (msec)");
                listItem.SubItems.Add(ActiveRequest.ExecutionTime.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Queued");
                listItem.SubItems.Add(ActiveRequest.Queued ? "Yes" : "No");
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Queued Time");
                listItem.SubItems.Add(ActiveRequest.QueuedTime.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Remote Computer");
                listItem.SubItems.Add(ActiveRequest.RemoteComputer);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Remote Port");
                listItem.SubItems.Add(ActiveRequest.RemotePort);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Host Id");
                listItem.SubItems.Add(HostId.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Server Activity Name");
                listItem.SubItems.Add(ServerActivityName);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Server Activity StartTime");
                listItem.SubItems.Add(ServerActivityStartTime.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Server Activity Status");
                listItem.SubItems.Add(ServerActivityStatus.ToString());
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("Server Activity Status Reason");
                listItem.SubItems.Add(ServerActivityStatusReason);
                DetailsListView.Items.Add(listItem);

                listItem = new ListViewItem("RemoteAddress");
                listItem.SubItems.Add(RemoteAddress);
                DetailsListView.Items.Add(listItem);

                if (ActiveRequest.Parameters != null && ActiveRequest.Parameters.Length > 0)
                {
                    PopulateParameters(ActiveRequest.Parameters);
                }
            }
            else
            {
                this.Hide();
                MessageBox.Show("Invalid active request or request does not exist.");
                this.Close();
            }
        }

        private void CancelRequestButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to cancel this request?", "Cancel Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    AdministrationWebServiceSoapClient client = new AdministrationWebServiceSoapClient("AdministrationWebServiceSoap", RemoteAddress);
                    client.CancelRequest(HostId, ActiveRequest.RequestId, "Cancelled by TFS Request Manager");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("An error occurred while cancelling this request:\r\n{0}\r\n{1}", ex.Message, ex.StackTrace),
                        "Error Cancelling Request",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error,
                        MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Private Methods

        private void PopulateParameters(KeyValueOfStringString[] parameters)
        {
            ParametersDataGridView.AutoGenerateColumns = true;
            ParametersBindingSource.DataSource = parameters;
            ParametersDataGridView.DataSource = ParametersBindingSource;
            ParametersDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            ParametersDataGridView.RowHeadersVisible = false;
        }

        #endregion
    }
}
